﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Urbania.World
{
    public class World
    {
        protected int w;
        protected int h;
        protected int nHexagons;
        private Tile[] hexs;

        public World(int width, int height)
        {
            w = width;
            h = height;
            nHexagons = w * h;
            hexs = new Tile[nHexagons];
            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    float xpos = x * Tile.tileTranslateWidth;
                    float ypos;
                    if (x % 2 == 0)
                    {
                        ypos = y * Tile.tileHeight;
                    }
                    else 
                    {
                        ypos = (y + 0.5f) * Tile.tileHeight;
                    }
                    hexs[getIndexOfHex(x, y)] = new Tile(new Vector2(xpos, ypos), TileType.Buil_Factory);
                }
            }
        }


        public void Render(GameTime gameTime, SpriteBatch spriteBatch)
        {
            for (int i = 0; i < nHexagons; i++)
            {
                hexs[i].Draw(gameTime, spriteBatch);
            }
        }

        public void Update(GameTime gameTime, Rectangle viewportBounds)
        {
            foreach (Tile t in hexs)
                t.Update(gameTime, viewportBounds);
        }

        private int getIndexOfHex(int xi, int yi)
        {
            if (xi >= 0 && xi < w && yi >= 0 && yi < h)
            {
                return (xi + w * yi);
            }
            else
            {
                return -1;
            }
        }

    }
}
